package com.tarzan.security.config;


import com.tarzan.redis.service.RedisService;
import com.tarzan.security.evaluator.CustomizePermissionEvaluator;
import com.tarzan.security.exception.UnauthorizedEntryPoint;
import com.tarzan.security.filter.LoginFilter;
import com.tarzan.security.filter.TokenAuthenticationFilter;
import com.tarzan.security.handler.CustomLogoutHandler;
import com.tarzan.security.manager.DefaultPasswordEncoder;
import com.tarzan.security.manager.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;

/**
 * @Package com.tarzan.security.config
 * @Title WebSecurityConfigurer
 * @Description TODO
 * @Author liujinshan
 * @CreateTime 2021/6/29 10:59
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class CustomWebSecurityConfigurer extends WebSecurityConfigurerAdapter {


    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private TokenManager tokenManager;
    @Autowired
    private DefaultPasswordEncoder defaultPasswordEncoder;
    @Autowired
    private RedisService redisService;
    @Autowired
    private CustomizePermissionEvaluator customizePermissionEvaluator;

    /**
     * 配置设置
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new UnauthorizedEntryPoint())
                .and().csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and().cors()
                .and().logout().logoutUrl("/logout")
                .addLogoutHandler(new CustomLogoutHandler(tokenManager,redisService)).and()
                .addFilter(new LoginFilter(authenticationManager(), tokenManager, redisService))
                .addFilter(new TokenAuthenticationFilter(authenticationManager(), tokenManager, redisService)).httpBasic();
    }

    /**
     * 密码处理
     * @param auth
     * @throws Exception
     */
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }


    /**
     * 加密方式
     */
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }



    /**
     * 注入自定义PermissionEvaluator  可以通过注解设置权限
     */
    @Bean
    public DefaultWebSecurityExpressionHandler userSecurityExpressionHandler() {
        DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
        handler.setPermissionEvaluator(new CustomizePermissionEvaluator());
        return handler;
    }

    /**
     * 配置哪些请求不拦截
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/api/**",
                "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**","/user/findUserInfoByUserName/**","menu/selectMenuPermsByUserId/*"
        );
    }
}
